<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * PayFeeModel数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-23> SoChishun Added.
 */
class PayfeeModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tcti_payfeerate';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array();

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'idtext' => array(
            'field' => array('payType as id', 'concat(payType,"(",feeName,")") as text'),
            'cache' => true,
        ),
        'export'=>array(
            'field'=>array("payType as '费率组',feeName as '费率名称',firstSec as '首次计费时长(秒)',firstRate as '首次计费金额',perSec as '分段计费时长(秒)',perRate as '分段计费金额'"),
        ),
        'item_list'=>array(
            'field'=>'id,payType,feeName,firstSec,firstRate,perSec,perRate',
            'order'=>'payType',
        )
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回scope结果列表
     * @param string $scope
     * @return array
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function scope_list($scope) {
        return $this->scope($scope)->select();
    }

    /**
     * 返回scope字段列表
     * @param string $scope
     * @return array
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function scope_fields($scope) {
        return $this->cache('payfee_idtext_fields')->getField($this->_scope[$scope]['field']);
    }

    /**
     * 返回管理员列表
     * @return type
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_payfee_list($where) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count($this->pk);      
        $list = $this->cache(true)->scope('item_list')->where($where)->order($orderby)->page($page)->select();
        return array('total' => $total, 'rows' => $list);
    }

}
